package com.thxy.jxgl.user.servlet;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.thxy.jxgl.bumen.manager.BumenManager;
import com.thxy.jxgl.filter.ToCh;
import com.thxy.jxgl.javaBean.BuMen;
import com.thxy.jxgl.javaBean.tuser;
import com.thxy.jxgl.mubiaoka.manager.JsffManager;
import com.thxy.jxgl.user.manager.UserManager;

/**
 * Servlet implementation class UserServlet
 */
public class UserServlet extends HttpServlet {
	@Override
	protected void service(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String method = request.getParameter("method") == null?"":request.getParameter("method");
		request.setCharacterEncoding("gbk");
        if("add".equals(method))
        {
            tuser user=(tuser)request.getSession().getAttribute("tuser");
            request.getSession().removeAttribute("tuser");
            String userid=user.getUserid();
            String username=ToCh.toChinese(user.getUsername());
            String bmno=user.getBmno();
            String gwno=user.getGwno();
            String sex=user.getSex();
            String intime=user.getIntime();
            String level=user.getLevel();
        	boolean ins=UserManager.getInstance().addUser(userid, username,bmno,gwno,sex ,intime, level);
        	request.getRequestDispatcher("/user/adduser.jsp").forward(request, response);
        }else if ("find".equals(method)) {
        	String uid=request.getParameter("userid");
        	String uname=request.getParameter("username");
        	String bmno=request.getParameter("bmno");
        	String gwno=request.getParameter("gwno");
        	String sex=request.getParameter("sex");
        	String intime=request.getParameter("intime");
        	List userList=new ArrayList();
        	userList=UserManager.getInstance().findUser(uid, uname, bmno, gwno, sex, intime);
        	request.setAttribute("userList",userList);
        	request.getRequestDispatcher("/user/showuser.jsp").forward(request, response);
		}else if ("deleuser".equals(method)) {
			String userid=request.getParameter("userno");
			UserManager.getInstance().deleUser(userid);
			request.getRequestDispatcher("/user/adduser.jsp").forward(request, response);
		}else if ("edituser".equals(method)) {
			String userid=request.getParameter("no");
			String username=request.getParameter("username");
			String bmno=request.getParameter("bmno");
        	String gwno=request.getParameter("gwno");
        	String sex=request.getParameter("sex");
        	String intime=request.getParameter("intime");
        	String level=request.getParameter("level");
			boolean upd=UserManager.getInstance().updateUser(userid, username,bmno,gwno,sex ,intime, level);
			request.getRequestDispatcher("/user/adduser.jsp").forward(request, response);
		}
        /*
		List bmList=new ArrayList();
		bmList=BumenManager.getInstance().getBumen();
		request.setAttribute("bmList",bmList );*/
		//request.getRequestDispatcher("/user/adduser.jsp").forward(request, response);

		}
}
